#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from common.common import check_command_injection
from common.exception.common_exception import ErrCodeException
from informix import LOGGER
from informix.comm.comm import check_path_user, identify_env_user
from informix.comm.const import JsonConstant, RegisterType, ErrorCode, InformixInfo
from informix.param.resource_base_param_parse import ResourceBaseParam


class ConnectParam(ResourceBaseParam):
    """
    连通性检查入参解析
    """

    def __init__(self, param_dict):
        super(ConnectParam, self).__init__(param_dict)
        self.db_user = identify_env_user() if identify_env_user() else InformixInfo.INFORMIX_USER

    def get_single_instance_name(self):
        return self.application.get(JsonConstant.NAME, "")

    def get_single_sqlhost(self):
        single_sqlhost = self.application.get(JsonConstant.EXTEND_INFO, {}).get(JsonConstant.SQLHOSTS_PATH, "")
        if single_sqlhost:
            if check_command_injection(single_sqlhost) or not \
                    check_path_user(single_sqlhost, self.db_user, self.db_user):
                LOGGER.error("Failed to verify the parameter(single_sqlhost) validity.")
                raise ErrCodeException(ErrorCode.ERROR_SQLHOSTS_FILE_NOT_EXIST.value)
        return single_sqlhost

    def get_single_onconfig(self):
        single_onconfig = self.application.get(JsonConstant.EXTEND_INFO, {}).get(JsonConstant.ONCONFIG_PATH, "")
        if single_onconfig:
            if check_command_injection(single_onconfig) or not \
                    check_path_user(single_onconfig, self.db_user, self.db_user):
                LOGGER.error("Failed to verify the parameter(single_onconfig) validity.")
                raise ErrCodeException(ErrorCode.ERROR_ONCONFIG_FILE_NOT_EXIST.value)
        return single_onconfig

    def get_sub_type(self):
        para = self.application.get(JsonConstant.SUB_TYPE)
        return para

    def _get_single_or_cluster(self):
        para = self.application.get(JsonConstant.SUB_TYPE)
        if para == JsonConstant.INFORMIX_SINGLE_INSTANCE:
            return RegisterType.SINGLE
        elif para == JsonConstant.INFORMIX_CLUSTER_INSTANCE:
            return RegisterType.CLUSTER
        return RegisterType.ERR_TYPE
